package edu.hcmiu.portal.modelhook;

import com.liferay.portal.ModelListenerException;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portlet.bookmarks.model.BookmarksFolder;
import edu.hcmiu.portal.model.hook_activity;
import edu.hcmiu.portal.model.hook_category;
import edu.hcmiu.portal.service.hook_activityLocalServiceUtil;
import edu.hcmiu.portal.service.hook_categoryLocalServiceUtil;
import edu.hcmiu.portal.service.hook_userLocalServiceUtil;
import java.util.Date;

public class BookmarkFolderListener implements ModelListener<BaseModel> {
    public String[] activityArr = {"CREATE","REMOVE","EDIT"};

    public void onAfterCreate(BaseModel model) throws ModelListenerException {
        try {
            BookmarksFolder bookmarkFolder = (BookmarksFolder) model;

            long bookmarkFolderId = bookmarkFolder.getFolderId();
            String actName = activityArr[0];
            String actSubject = bookmarkFolder.getName();
            long userID = bookmarkFolder.getUserId();
            Date actTime = bookmarkFolder.getCreateDate();
            hook_activity activity = hook_activityLocalServiceUtil.createhook_activity(bookmarkFolderId);

            //activity.setActID(auto)
            activity.setActName(actName);
            activity.setActSubject(actSubject);
            activity.setActTime(actTime);
            activity.setUserID(userID);
            hook_activityLocalServiceUtil.addhook_activity(activity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void onAfterAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onAfterRemove(BaseModel model) throws ModelListenerException {
        try {


        try {
            BookmarksFolder bookmarkFolder = (BookmarksFolder) model;

            long bookmarkFolderId = bookmarkFolder.getFolderId();
            System.out.println("FolderId: " + bookmarkFolderId);
            String actName = activityArr[1];
            String actSubject = bookmarkFolder.getName();
            long userID = bookmarkFolder.getUserId();
            Date actTime = bookmarkFolder.getModifiedDate();
            hook_activity activity = hook_activityLocalServiceUtil.createhook_activity(bookmarkFolderId);

            //activity.setActID(auto)
            activity.setActName(actName);
            activity.setActSubject(actSubject);
            activity.setActTime(actTime);
            activity.setUserID(userID);
            hook_activityLocalServiceUtil.addhook_activity(activity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        } catch (Exception e) {
        }
    }

    public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onAfterUpdate(BaseModel model) throws ModelListenerException {
        try {
            BookmarksFolder bookmarkFolder = (BookmarksFolder) model;

            long bookmarkFolderId = bookmarkFolder.getFolderId();
            System.out.println("FolderId: " + bookmarkFolderId);
            String actName = activityArr[2];
            String actSubject = bookmarkFolder.getName();
            long userID = bookmarkFolder.getUserId();
            Date actTime = bookmarkFolder.getModifiedDate();
            hook_activity activity = hook_activityLocalServiceUtil.createhook_activity(bookmarkFolderId);

            //activity.setActID(auto)
            activity.setActName(actName);
            activity.setActSubject(actSubject);
            activity.setActTime(actTime);
            activity.setUserID(userID);
            hook_activityLocalServiceUtil.addhook_activity(activity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeCreate(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeRemove(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeUpdate(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

}